package evan.curtis.pi_player;

import java.io.IOException;
import com.jcraft.jsch.JSchException;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PiFilesFragment extends Fragment {

	private View mContentView;
	private MainActivity mActivity;
	private LinearLayout m_layout;
	private MenuItem playPause;
	private MenuItem stop;
	private MenuItem restartAlsa;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mContentView = inflater.inflate(R.layout.blank_linearlayout, container,
				false);

		mActivity = (MainActivity) this.getActivity();

		m_layout = (LinearLayout) mContentView.findViewById(R.id.linearlayout);
		
		setHasOptionsMenu(true);

		showMusicLibrary();

		return mContentView;
	}

	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		playPause = menu.add("Play");
		playPause.setOnMenuItemClickListener(menuItemListener);
		
		stop = menu.add("Stop");
		stop.setOnMenuItemClickListener(menuItemListener);

		restartAlsa = menu.add("Restart ALSA");
		restartAlsa.setOnMenuItemClickListener(menuItemListener);

	}

	private OnMenuItemClickListener menuItemListener = new OnMenuItemClickListener() {

		public boolean onMenuItemClick(MenuItem m) {
			if (m.getTitle().equals("Stop")) {
				new sshTask().execute("pkill -SIGHUP mpg321");
				playPause.setTitle("Play");
			} else if (m.getTitle().equals("Play")) {
				new sshTask().execute("pkill -SIGCONT mpg321");
				playPause.setTitle("Pause");
			} else if (m.getTitle().equals("Pause")) {
				new sshTask().execute("pkill -SIGSTOP mpg321");
				playPause.setTitle("Play");
			}
			return false;
			
		}
	};

	private void showMusicLibrary() {

		new sshTask().execute("cd Music\nls");

	}

	
	public class sshTask extends AsyncTask<String, Integer, String> {

		String command = "";
		ProgressBar pBar;
		AlertDialog dialog = null;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			Context c = mActivity;
			dialog = new AlertDialog.Builder(c).create();
			dialog.setTitle("Wait.");
			LinearLayout linear = new LinearLayout(c);
			linear.setOrientation(LinearLayout.VERTICAL);
			dialog.setView(linear);
			LayoutInflater inflater = (LayoutInflater) c
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View volumeView = inflater.inflate(R.layout.wait_dialog, linear);
			pBar = (ProgressBar) volumeView.findViewById(R.id.progressBar);
			dialog.show();

		}

		@Override
		protected String doInBackground(String... params) {
			
			String task = params[0];
			command = task;
			String s = "";
			try {
				s = mActivity.sshCommand(task);

			} catch (JSchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Log.i("iofail", "fail");
				e.printStackTrace();
			}

			return s;
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
			super.onProgressUpdate(progress);
			pBar.setProgress(progress[0]);

		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			
			dialog.cancel();

			if (command.equals("cd Music\nls")) {

				int songNum = 0;
				int color = getResources().getColor(R.color.text_view_color);
				int alternateColor = getResources().getColor(
						R.color.text_view_alternate);

				String[] files = result.split("\n");
				for (int i = 0; i < files.length; i++) {

					TextView t = new TextView(mActivity.getApplicationContext());
					t.setText(files[i]);
					t.setOnClickListener(fileOnClickListener);
					t.setTextSize(20);
					t.setBackgroundColor(color);

					// set every other textView to the alternate color
					if (songNum % 2 == 0) {
						t.setBackgroundColor(alternateColor);

					}
					songNum++;

					m_layout.addView(t);
					

				}
			}
		}
	}

	private OnClickListener fileOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			// get filename from TextView
			TextView t = (TextView) v;
			String filename = t.getText().toString();
			// add escape characters
			filename = filename.replace(" ", "\\ ");

			
			String stop = "pkill -SIGHUP mpg321";
			String playSong = "cd Music\nmpg321 " + filename;
			
			new sshTask().execute(stop + "\n" + playSong);
			
			playPause.setTitle("Pause");

		}
	};

}
